<template>
  <div class="login_container">
    <div class="login_form">
      <p class="login_title">客户关系管理系统</p>
      <el-form
        :model="form"
        ref="form"
        :rules="rules"
        status-icon
        class="demo-ruleForm"
      >
        <el-form-item class="from_item1" prop="username">
          <el-input
            size="medium"
            v-model="form.username"
            placeholder="请输入账号"
            prefix-icon="el-icon-user-solid"
          >
          </el-input>
        </el-form-item>
        <el-form-item class="from_item2" prop="password">
          <el-input
            size="medium"
            type="password"
            v-model="form.password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item class="from_item4">
          <el-checkbox
            class="rememberPwd"
            v-model="rememberPwd"
            label="1"
            size="small"
            >记住密码</el-checkbox
          >
        </el-form-item>
        <el-form-item>
          <el-button
            class="login_button"
            type="primary"
            size="medium"
            @click="submitFormData()"
            :loading="Loading"
            >登录</el-button
          >
        </el-form-item>
        <div>
          <Vcode
            :show="captchaIsShow"
            @success="captchaSuccess"
            @close="captchaClose"
          ></Vcode>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import Vcode from "vue-puzzle-vcode";
export default {
  name: "Login",
  components: {
    Vcode,
  },
  data() {
    return {
      Loading:false,
      captchaIsShow: false,
      rememberPwd: "1",
      form: {
        username: "",
        password: "",
        code: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    captchaSuccess(msg) {
      this.captchaIsShow = false;
      this.loginHandle();
    },
    captchaClose() {
      this.captchaIsShow = false;
    },
    submitFormData() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.captchaIsShow = true;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    loginHandle() {
      this.Loading=true
      this.$http.post("/login", this.form).then((res) => {
        console.log(res);
        if (res.data.code == 200) {
          sessionStorage.setItem("token", res.headers.authorization);
          sessionStorage.setItem("username", this.form.username);
          this.Loading=false
          this.$router.push("/");
        } else {
          this.Loading=false
          this.$message.error(res.data.msg);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.login_container {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100vh;
  background-color: rgb(255, 255, 255);
  background-image: url("@/assets/image/backgroud/pexels-小鸿-刘-13286254.jpg");
  background-position: center bottom;
  background-repeat: no-repeat;
  background-attachment: fixed;
  background-size: 1873px 970px;
  background-origin: border-box;
  border: none;
  border-radius: 0px;
  .login_form {
    border-radius: 6px;
    width: 360px;
    height: 350px;
    margin: 0px auto;
    padding: 25px 25px 5px 25px;
    background-color: #ffffffef;
    border: none;
    border-radius: 5px;
    // 添加阴影效果
    box-shadow: 0 0 25px #cac6c6;
    text-align: center;
    .from_item1 {
      padding: 0px 10px;
    }
    .from_item2 {
      padding: 0px 10px;
    }
    .from_item3 {
      padding: 0px 10px;
    }
    .rememberPwd {
      padding: 0px 10px;
      float: left;
    }
    .login_title {
      font-family: "微软雅黑 Bold", "微软雅黑";
      font-weight: 700;
      text-decoration: none;
      color: rgb(126, 132, 138);
      font-size: 25px;
      margin-top: 30px;
      margin-bottom: 30px;
      text-align: center;
    }
  }
}
</style>